# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gtk
import pango

from AdventureMoney.core import Utils
import InterfaceUtils

class PeopleView(gtk.Frame):
	def __init__(self, account):
		gtk.Frame.__init__(self)
		self.wTree = InterfaceUtils.GetGladeTree("PeopleViewHolder")
		
		signals = {
			"OnNewPerson"		: self.OnNewPerson,
			"OnDeletePerson"	: self.OnDeletePerson,
			"OnSetManager"		: self.OnSetManager,
			"OnTreeViewSelection"	: self.OnTreeViewSelection
		}
		
		self.wTree.signal_autoconnect(signals)
		
		self.vbox = self.wTree.get_widget("PeopleViewVBox")
		self.wTree.get_widget("PeopleViewHolder").remove(self.vbox)
		self.add(self.vbox)
		
		self.account = account
		self.account.connect("people", self.UpdateInterface)
		self.account.connect("manager", self.UpdateInterface)
		
		self.treeview = self.wTree.get_widget("PeopleTreeView")
		self.deleteButton = self.wTree.get_widget("DeletePersonButton")
		self.newButton = self.wTree.get_widget("NewPersonButton")
		self.closeButton = self.wTree.get_widget("CloseButton")
		self.setManagerButton = self.wTree.get_widget("SetManagerButton")
		
		self.model = gtk.ListStore(str, str)
		self.treeview.set_model(self.model)
		
		cell = gtk.CellRendererText()
		cell.set_property("editable", True)
		cell.connect("edited", self.OnEditedPerson)
		columnName = gtk.TreeViewColumn("Name")
		columnName.pack_start(cell)
		columnName.set_attributes(cell, text=0)
		
		cell = gtk.CellRendererText()
		cell.set_property("style", pango.STYLE_ITALIC)
		columnManager = gtk.TreeViewColumn("Manager")
		columnManager.pack_end(cell, False)
		columnManager.set_attributes(cell, text=1)
		
		self.treeview.append_column(columnName)
		self.treeview.append_column(columnManager)
		
		#desensitize widgets
		self.OnTreeViewSelection()
		
		self.show_all()
	
	def OnNewPerson(self, widget=None):
		name = self.MakeUniqueName("New Person")
			
		self.account.addPerson(name)
	
	def OnEditedPerson(self, cellRenderer, path, newText):
		oldName = self.model[path][0]
		if oldName != newText:
			name = self.MakeUniqueName(newText)
			self.account.renamePerson(oldName, name)
	
	def OnDeletePerson(self, widget):
		name = self.GetSelectedName()
		if name:
			self.account.removePerson(name)
	
	def OnSetManager(self, widget):
		name = self.GetSelectedName()
		if name:
			person = self.account.findPerson(name)
			self.account.setManager(person)
		
	def OnTreeViewSelection(self, widget=None):
		selected = (self.treeview.get_cursor()[0] != None)
		for i in (self.deleteButton, self.setManagerButton):
			i.set_sensitive(selected)
	
	def GetSelectedName(self):
		path = self.treeview.get_cursor()[0]
		if path:
			row = self.model[path][0]
			return row
		return None
		
	def MakeUniqueName(self, requestName):
		name = requestName.replace("|", "")
		count = 1
		while self.account.personExists(name):
			name = "%s (%d)" % (requestName, count)
			count += 1
			
		return name
	
	def UpdateModel(self):
		path = self.treeview.get_cursor()[0]
		
		self.model.clear()
		for person in self.account.getPeople():
			manager = ""
			if self.account.isManager(person):
				manager = "(Manager)"
			self.model.append((person.getName(), manager))
			
		if path and len(self.model):
			if path[0] >= len(self.model):
				index = len(self.model) - 1
			else:
				index = path[0]
			self.treeview.set_cursor( (index,) )
		
	def UpdateInterface(self, widget=None):
		self.UpdateModel()
